<?php

    class Custom_Fields_Controller extends Application {
        
        function setup()
        {
            $this->actions = array('index','view','modify','delete','entries_custom_field');
            $this->before_filter('is_logged_in');
            $this->before_filter('has_access');
			$subnav['All Fields'] = 'index';
			$subnav['Add New Custom Field'] = 'modify';
			$this->assign('subnav',$subnav);

			use_model('custom_fields');
			$this->use_layout('admin_layout.php');						
        }
        
        function has_access()
        {
            if (!has_access_to('Custom Fields::Manage Fields')) {
                flash('error','Sorry but you do not have access to the Custom Fields tool');
                $this->redirect_to(array('controller'=>'admin/dashboard'));            
            }
        }
        
        function index()
        {
            $custom_fields = new custom_fields();
            $this->assign('report',$custom_fields->get_paged_report());
        }

        function modify()
        {
            $custom_fields = new custom_fields();
            $this->assign_ref('custom_fields',$custom_fields);
    
            if ( isset($_GET['id']) )
            {        
                $id = (int) $_GET['id'];
                $custom_fields->load($id);
            }
            if ( $this->is_post() )
            {
                $id = (int) $_POST['custom_fields']['id'];
                $custom_fields->load($id);                
                if ( $custom_fields->update_fields($_POST['custom_fields']) )
                {
                    flash('notice','Item Saved');
                    $this->redirect_to('index');
                } else {
                    flash('error','Item could not be saved');
                }
            }
        }
        
        // for pages or news or whatevahs
        function entries_custom_field()
        {
            // delete a custom field
            if ($this->is_post() && $_POST['action'] == 'delete') {
                use_model('entries_custom_fields');
                $custom_fields = new entries_custom_fields();
                if ($custom_fields->load((int)$_POST['entries_custom_fields']['id'])) {
                    $custom_fields->delete();
                    $this->render(false);
                    return;
                }
            }
            // only to be done via ajax. cause why the heck not.
            if ($this->is_ajax_request()) {
                use_model('entries_custom_fields');
                $custom_fields = new entries_custom_fields();
                // adding a new custom field for a page or news item
                if ($this->is_post()) {
                    $custom_fields->load((int)$_POST['entries_custom_fields']['id']);
                    if ($custom_fields->update_fields($_POST['entries_custom_fields'])) {
                        flash('notice','Field data saved');
                        $this->assign_ref('field',$custom_fields);
                        // value should be page_id or news_id
                        $this->assign('related_key',$_GET['related_key']);
                        // value should be the id of post or page
                        $this->assign('related_value', (int) $_GET['related_value']);
                        $this->render(array('partial'=>'_update_field.php'));
                    } else {
                        flash('error','Field could not be saved ');
                        $this->render(array('inline'=>'Field failed to save. '. $custom_fields->errors_to_string()));
                    }
                } else {
                    // get a custom field to insert into the pages or news area.
                    // it will use any default values set up in the custom fields section
                    if (!empty($_GET['id'])) {
                        $custom_fields = new custom_fields();
                        if ($custom_fields->load( (int) $_GET['id'])) {
                            $this->assign_ref('field',$custom_fields);
                            // value should be page_id or news_id
                            $this->assign('related_key',$_GET['related_key']);
                            // value should be the id of post or page
                            $this->assign('related_value', (int) $_GET['related_value']);
                            
                            $this->render(array('partial'=>'_insert_field.php'));
                            return;
                        }
                    }
                    $this->render(array('inline'=>'Field failed to load.'));
                }                              
            } else {
                $this->redirect_to('index');
            }
        }

        function view()
        {
            if ( isset($_GET['id']) )
            {
                $custom_fields = new custom_fields();
                $id = (int) $_GET['id'];
                if ( $custom_fields->load($id) )
                {
                    $this->assign('fields',$custom_fields->fields);
                } else {
                    flash('error','Item could not be loaded');
                    $this->redirect_to('index');
                }
            } else {
                flash('error','Item could not be loaded');
                $this->redirect_to('index');
            }
        }

        function delete()
        {
            if ( $this->is_post() && isset($_POST['id']) ) {
                $custom_fields = new custom_fields();
                $id           = (int) $_POST['id'];
                if ( $custom_fields->load($id) )
                {
                    $custom_fields->delete();
                    flash('notice','Item Deleted');
                } else {
                    flash('error','Item could not be deleted');
                }
            }
            $this->redirect_to('index'); 
        }

        
    }

?>